﻿using DocumentFormat.OpenXml.Office2010.Excel;
using Domain.Material_Management;
using Domain.Production_Modules;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Dto.Warehouse;
using ReturnValue;

namespace MES.Server.API.Application.Command.Production_Modules.GroupInventory
{
    public class GetGroupInventoryHander : IRequestHandler<GetGroupInventoryCommand, ApiPaging<GroupInventoryOutPut>>
    {
        private readonly IRepository<GroupInventoryModel> groupInventoryRepo;
        private readonly IRepository<MaterialGroupModel> materialGroupRepo;
        private readonly IRepository<WarehouseModel> warehouseRepo;
        public GetGroupInventoryHander(IRepository<GroupInventoryModel> groupInventoryRepo, IRepository<MaterialGroupModel> materialGroupRepo, IRepository<WarehouseModel> warehouseRepo)
        {
            this.groupInventoryRepo = groupInventoryRepo;
            this.materialGroupRepo = materialGroupRepo;
            this.warehouseRepo = warehouseRepo;
        }
        /// <summary>
        /// 群组库存列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiPaging<GroupInventoryOutPut>> Handle(GetGroupInventoryCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiPaging<GroupInventoryOutPut>();
            var groupInventory = groupInventoryRepo.GetAll().Where(x=>x.IsDel==false);
            var materialGroup = materialGroupRepo.GetAll().Where(x => x.IsDel == false);
            var warehouse = warehouseRepo.GetAll().Where(x => x.IsDel == false);
            var linq = from g in groupInventory
                       join m in materialGroup on g.MaterialGroupId equals m.Id
                       join w in warehouse on g.WarehouseId equals w.Id
                       select new GroupInventoryOutPut
                       {
                            Id = g.Id   ,
                            WarehouseId = w.Id ,
                            WarehouseName = w.WarehouseName ,
                            CurrentInventory = g.CurrentInventory ,
                            InventoryCap = g.InventoryCap  , 
                            InventoryFloor = g.InventoryCap,
                            MaterialGroupId = m.Id,
                            MaterialGroupCode = m.MaterialGroupCode,
                            MaterialGroupName = m.MaterialGroupName,
                       };
            result.TotalPage = linq.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Totalcount = linq.Count();
            result.Code = StatusCode.Success;
            return Task.FromResult(result);
        }
    }
}
